<%--
  Created by IntelliJ IDEA.
  User: KangFaWei
  Date: 2019/6/23
  Time: 10:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<jsp:include page="/web/inc/header"/>
<section>
    <div class="wrap">
        <div id="seach">
            <div id="logo" class="qanda"><a href="http://fenlicx.qinglab.cn" title="再搜索垃圾分类问答">再搜索垃圾分类问答</a></div>
            <div class="crumbs">
                <a href="<%=path%>/web/question/list" >&lt; 返回问答列表</a>
                <%--<a href="<%=path%>/web/question/list">&lt;&lt;&lt;返回</a>--%>
                <%--<form>--%>
                <%--<input type="text" name="seach" class="seach_txt">--%>
                <%--<input type="submit" class="seach_bun" name="button" value="查询分类">--%>
                <%--</form>--%>
            </div>
        </div>
        <nav></nav>
        <article class="article">
            <div class="comment">
                <h1>${requestScope.question.title}
                    <c:if test="${requestScope.question.finished}"><span>(${requestScope.question.answerCount})条答复</span><span class="show-finished">已完成</span></c:if>
                </h1>
                <%--<span>${requestScope.question.}</span>--%>
                <div class="comment-input commentList">
                </div>
                <c:if test="${(not requestScope.question.finished) and (not (empty sessionScope.userinfo))}">
                    <form id="myForm" method="post" action="<%=path%>/web/answer/insert">
                        <input type="hidden" name="question.id" value="${requestScope.question.id}"/>
                        <textarea id="content" name="content"></textarea>
                        <input id="description" type="hidden" name="description">
                        <input id="publish-answer" type="button" value="发布"/>
                    </form>
                </c:if>
            </div>
        </article>
    </div>
</section>
<script src="<%=path%>/static/js/jquery-3.3.1.min.js"></script>
<!-- 配置文件 -->
<script type="text/javascript" src="<%=path%>/ueditor/ueditor.config.js"></script>
<!-- 编辑器源码文件 -->
<script type="text/javascript" src="<%=path%>/ueditor/ueditor.all.js"></script>
<!-- 实例化编辑器 -->
<script>
    var ue = UE.getEditor('content', {
        toolbars: [
            [
                'bold',//加粗
                'italic',//斜体
                'superscript', //上标
                'subscript', //下标
                'indent', //首行缩进
                'simpleupload', //单图上传
                'link', //超链接
            ]
        ]
    });
    ue.ready(function () {
        ue.setHeight(100);
    })


    $(function () {
        $("#publish-answer").click(function () {
            var description = ue.getContentTxt();
            $("#description").val(description);
            if(description.trim().length!=0){
                $("#myForm").submit();
            }

        })
        
        
        var finished =${requestScope.question.finished};
        var id = ${requestScope.question.userinfo.id};

        $(".comment-input").on("click", ".accept", function () {
            var answerId = $(this).data("id");
            var questionId = ${requestScope.question.id};
            $.post(
                "<%=path%>/web/answer/finished",
                {id: answerId, "question.id": questionId},
                function () {
                    getAnswer(1, questionId, true, id);
                    $("h1").append("<span>已完成</span>")
                    $("#content").css("display", "none");
                    $("#publish-answer").css("display", "none");
                }
            )
        })

        getAnswer(1, ${requestScope.question.id}, finished, id);

        //获取回答数据
        function getAnswer(pageNum, questionId, finished, id) {

            $.post(
                "<%=path%>/web/question/answerData",
                {pageNum: pageNum, questionId: questionId, finished: finished, userinfoId: id},
                function (mes) {
                    $(".comment-input").html(mes);
                }
            )
        }

        //    翻页
        $(".comment-input").on("click", ".show-page", function () {
            var pageNum = $(this).html();
            getAnswer(pageNum, ${requestScope.question.id}, finished, id);
        })

    })
</script>
<jsp:include page="/web/inc/footer"/>